<?php
/**
 * Order Dao definition 
 *
 * @author guzhi
 * @since 2009-04-10
 */
require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';
class OrderDao extends BaseDao
{
    protected $_name;
    public function _setup ()
    {
        $this->_name = $this->prefix() . "orders";
        parent::_setup();
    }
    public function addOrder ($order)
    {
        return $this->insertData($order);
    }
    public function updateOrder ($id, $order)
    {
        if(is_numeric($id)){
            $this->updateData($order, "id=$id",true);
        }
    }
    public function deleteOrder ($id)
    {
        $isDel = array(
            "is_delete" => 1
        );
        $where = $this->db_w->quoteInto('id = ?', $id);
        $this->updateData($isDel, $where);
    }
    public function getOrders ()
    {
        $select = $this->db_r->select();
        $select->from($this->_name, "*");
        $select->where("is_delete = 0");
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }
    public function getOrderById ($id)
    {
        if(is_numeric($id)){
            $sql = "select	{$this->_name}.*,
            				u1.name user_name,
            				u2.name agent_name,
            				u2.addr agent_addr,
            				u2.full_name full_name,
                            u2.postal_code agent_postal_code,
                            u2.mobile agent_mobile,
                            u2.phone agent_phone,
                            u2.district agent_district,
                            u2.city agent_city,
            				agent_campuses.area_name,
            				ad1.title datitle1,
            				ad2.title datitle2,
            				ad3.title datitle3
            		from	{$this->_name}
            		inner	join users u1 on u1.id = {$this->_name}.user_id
            		left	outer join users u2 on u2.id = {$this->_name}.agent_id
            		left	outer join agent_campuses on agent_campuses.id = {$this->_name}.agent_campus_id
            		left	outer join area_dicts ad1 on ad1.id = {$this->_name}.delivery_address_1
            		left	outer join area_dicts ad2 on ad2.id = {$this->_name}.delivery_address_2
            		left	outer join area_dicts ad3 on ad3.id = {$this->_name}.delivery_address_3
            		where	{$this->_name}.id = {$id}
            		";
            return $this->db_r->fetchRow($sql);
        }
        return array();
        $select = $this->db_r->select();
        $select->from($this->_name, '*');
        $select->join('addresses', $this->_name . '.shipping_address_id=addresses.id', array(
            'city' , 
            'state' , 
            'country' , 
            'zip_code'
        ));
        $select->where($this->_name . '.id=?', $id);
        $sql = $select->__toString();
        return $this->db_r->fetchRow($sql);
    }
    public function getAllById ($id)
    {
        $shipCols = array(
            'id as s_id' , 
            'first_name as s_first_name' , 
            'last_name as s_last_name' , 
            'address1 as s_address1' , 
            'address2 as s_address2' , 
            'city as s_city' , 
            'state as s_state' , 
            'country as s_country' , 
            'zip_code as s_zip_code' , 
            'phone as s_phone'
        );
        $billCols = array(
            'id as b_id' , 
            'first_name as b_first_name' , 
            'last_name as b_last_name' , 
            'address1 as b_address1' , 
            'address2 as b_address2' , 
            'city as b_city' , 
            'state as b_state' , 
            'country as b_country' , 
            'zip_code as b_zip_code' , 
            'phone as b_phone'
        );
        $select = $this->db_r->select();
        $select->from($this->_name, '*');
        $select->join('addresses', $this->_name . '.shipping_address_id=addresses.id', $shipCols);
        $select->join('addresses', $this->_name . '.billing_address_id=addresses.id', $billCols);
        $select->where($this->_name . '.id=?', $id);
        $select->where($this->_name . '.is_delete=0');
        $sql = $select->__toString();
        return $this->db_r->fetchRow($sql);
    }
    public function countOrders ()
    {
        $select = $this->db_r->select();
        $select->from($this->_name, array(
            'count(id) as num' , 
            'status'
        ));
        $select->group('status');
        $select->where('is_delete = 0');
        $sql = $select->__toString();
        return $this->db_r->fetchAll($sql);
    }
    
}